// 人格基础类型
export interface Persona {
  id: string;
  name: string;
  displayName?: string;
  description: string;
  avatar?: string;
  coverImage?: string;
  category: string;
  tags: string[];
  version: string;
  status: "active" | "inactive" | "draft" | "archived";
  visibility: "public" | "private" | "unlisted";
  featured: boolean;
  verified: boolean;
  createdBy: string;
  createdAt: string;
  updatedAt: string;
  publishedAt?: string;
  config: PersonaConfig;
  stats: PersonaStats;
  metadata?: Record<string, any>;
}

// 人格配置类型
export interface PersonaConfig {
  // 基础设置
  systemPrompt: string;
  greeting?: string;
  personality: {
    traits: string[];
    tone:
      | "formal"
      | "casual"
      | "friendly"
      | "professional"
      | "playful"
      | "serious";
    style: "concise" | "detailed" | "conversational" | "academic" | "creative";
    expertise: string[];
    interests: string[];
    background?: string;
    goals?: string[];
    limitations?: string[];
  };

  // 模型参数
  model: {
    name: string;
    provider: "openai" | "anthropic" | "google" | "local" | "custom";
    temperature: number;
    maxTokens: number;
    topP: number;
    topK?: number;
    frequencyPenalty: number;
    presencePenalty: number;
    stopSequences?: string[];
    seed?: number;
  };

  // 功能设置
  capabilities: {
    textGeneration: boolean;
    imageGeneration: boolean;
    imageAnalysis: boolean;
    audioGeneration: boolean;
    audioAnalysis: boolean;
    codeGeneration: boolean;
    webSearch: boolean;
    knowledgeBase: boolean;
    functionCalling: boolean;
    fileProcessing: boolean;
    multimodal: boolean;
  };

  // 知识库设置
  knowledge: {
    baseIds: string[];
    searchEnabled: boolean;
    citationRequired: boolean;
    maxSources: number;
    relevanceThreshold: number;
  };

  // 安全设置
  safety: {
    level: "low" | "medium" | "high" | "strict";
    contentFilter: boolean;
    toxicityFilter: boolean;
    biasFilter: boolean;
    personalInfoFilter: boolean;
    allowedTopics?: string[];
    blockedTopics?: string[];
    customRules?: string[];
  };

  // 记忆设置
  memory: {
    enabled: boolean;
    type: "conversation" | "session" | "persistent" | "none";
    maxEntries: number;
    retentionDays: number;
    autoSummarize: boolean;
    contextWindow: number;
  };

  // 交互设置
  interaction: {
    responseDelay: number;
    typingIndicator: boolean;
    maxResponseLength: number;
    allowInterruption: boolean;
    confirmActions: boolean;
    suggestFollowups: boolean;
    enableEmotions: boolean;
    enableGestures: boolean;
  };

  // 外观设置
  appearance: {
    avatar: {
      type: "2d" | "3d" | "vtuber" | "static";
      url?: string;
      animations?: string[];
      expressions?: Record<string, string>;
    };
    voice: {
      enabled: boolean;
      provider?: "elevenlabs" | "azure" | "google" | "amazon" | "custom";
      voiceId?: string;
      speed: number;
      pitch: number;
      volume: number;
      language: string;
    };
    theme: {
      primaryColor: string;
      secondaryColor: string;
      backgroundColor: string;
      textColor: string;
      accentColor: string;
    };
  };

  // 高级设置
  advanced: {
    customInstructions?: string;
    preProcessing?: string;
    postProcessing?: string;
    errorHandling?: string;
    fallbackBehavior?: "default" | "silent" | "error" | "redirect";
    rateLimiting?: {
      enabled: boolean;
      maxRequests: number;
      timeWindow: number;
    };
    analytics?: {
      enabled: boolean;
      trackUsage: boolean;
      trackPerformance: boolean;
      trackSatisfaction: boolean;
    };
  };
}

// 人格统计类型
export interface PersonaStats {
  usage: {
    totalSessions: number;
    totalMessages: number;
    totalUsers: number;
    avgSessionLength: number;
    avgResponseTime: number;
    lastUsed?: string;
  };
  performance: {
    successRate: number;
    errorRate: number;
    avgSatisfactionScore: number;
    responseQuality: number;
    userRetention: number;
  };
  popularity: {
    views: number;
    likes: number;
    dislikes: number;
    favorites: number;
    shares: number;
    downloads: number;
    rating: {
      average: number;
      count: number;
      distribution: Record<number, number>;
    };
  };
  engagement: {
    avgMessagesPerSession: number;
    avgSessionDuration: number;
    returnUserRate: number;
    recommendationRate: number;
  };
}

// 人格分类类型
export interface PersonaCategory {
  id: string;
  name: string;
  description?: string;
  icon?: string;
  color?: string;
  parentId?: string;
  children?: PersonaCategory[];
  personaCount: number;
  featured: boolean;
  order: number;
  createdAt: string;
  updatedAt: string;
}

// 人格标签类型
export interface PersonaTag {
  id: string;
  name: string;
  description?: string;
  color?: string;
  category?: string;
  usageCount: number;
  trending: boolean;
  createdAt: string;
}

// 人格模板类型
export interface PersonaTemplate {
  id: string;
  name: string;
  description: string;
  category: string;
  tags: string[];
  config: Partial<PersonaConfig>;
  preview?: {
    avatar?: string;
    sampleConversation: Array<{
      role: "user" | "assistant";
      content: string;
    }>;
  };
  difficulty: "beginner" | "intermediate" | "advanced";
  estimatedSetupTime: number;
  requirements?: string[];
  instructions?: string[];
  isOfficial: boolean;
  createdBy: string;
  createdAt: string;
  updatedAt: string;
  usageCount: number;
  rating: {
    average: number;
    count: number;
  };
}

// 人格版本类型
export interface PersonaVersion {
  id: string;
  personaId: string;
  version: string;
  name: string;
  description?: string;
  config: PersonaConfig;
  changelog?: string;
  isActive: boolean;
  isStable: boolean;
  createdBy: string;
  createdAt: string;
  downloadCount: number;
  compatibility: {
    minVersion: string;
    maxVersion?: string;
    platforms: string[];
  };
}

// 人格评价类型
export interface PersonaReview {
  id: string;
  personaId: string;
  userId: string;
  username: string;
  avatar?: string;
  rating: number;
  title?: string;
  content: string;
  pros?: string[];
  cons?: string[];
  usageContext?: string;
  recommendedFor?: string[];
  helpful: number;
  reported: boolean;
  verified: boolean;
  createdAt: string;
  updatedAt: string;
  replies?: PersonaReviewReply[];
}

// 人格评价回复类型
export interface PersonaReviewReply {
  id: string;
  reviewId: string;
  userId: string;
  username: string;
  avatar?: string;
  content: string;
  isAuthor: boolean;
  helpful: number;
  createdAt: string;
  updatedAt: string;
}

// 人格收藏类型
export interface PersonaFavorite {
  id: string;
  userId: string;
  personaId: string;
  tags?: string[];
  notes?: string;
  createdAt: string;
}

// 人格分享类型
export interface PersonaShare {
  id: string;
  personaId: string;
  shareId: string;
  title: string;
  description?: string;
  isPublic: boolean;
  password?: string;
  expiresAt?: string;
  allowDownload: boolean;
  allowFork: boolean;
  viewCount: number;
  downloadCount: number;
  createdBy: string;
  createdAt: string;
  updatedAt: string;
}

// 人格市场类型
export interface PersonaMarketplace {
  featured: Persona[];
  trending: Persona[];
  recent: Persona[];
  categories: PersonaCategory[];
  tags: PersonaTag[];
  stats: {
    totalPersonas: number;
    totalDownloads: number;
    totalUsers: number;
    avgRating: number;
  };
}

// 人格搜索结果类型
export interface PersonaSearchResult {
  personas: Array<{
    persona: Persona;
    relevanceScore: number;
    matchedFields: string[];
    highlights: Record<string, string[]>;
  }>;
  facets: {
    categories: Array<{ name: string; count: number }>;
    tags: Array<{ name: string; count: number }>;
    ratings: Array<{ rating: number; count: number }>;
    creators: Array<{ name: string; count: number }>;
  };
  total: number;
  page: number;
  totalPages: number;
  queryTime: number;
}

// 人格使用会话类型
export interface PersonaSession {
  id: string;
  personaId: string;
  userId: string;
  sessionId: string;
  startedAt: string;
  endedAt?: string;
  messageCount: number;
  duration: number;
  satisfaction?: number;
  feedback?: string;
  metadata?: Record<string, any>;
}

// 人格分析类型
export interface PersonaAnalytics {
  personaId: string;
  timeRange: {
    start: string;
    end: string;
  };
  usage: {
    sessions: Array<{ date: string; count: number }>;
    messages: Array<{ date: string; count: number }>;
    users: Array<{ date: string; count: number }>;
    avgSessionDuration: number;
    peakHours: number[];
  };
  performance: {
    responseTime: Array<{ date: string; avg: number; p95: number }>;
    errorRate: Array<{ date: string; rate: number }>;
    satisfactionScore: Array<{ date: string; score: number }>;
  };
  demographics: {
    userTypes: Record<string, number>;
    locations: Record<string, number>;
    devices: Record<string, number>;
    languages: Record<string, number>;
  };
  content: {
    topTopics: Array<{ topic: string; count: number }>;
    avgMessageLength: number;
    languageDistribution: Record<string, number>;
    sentimentDistribution: Record<string, number>;
  };
}

// 人格导出类型
export interface PersonaExport {
  format: "json" | "yaml" | "zip";
  includeConfig: boolean;
  includeStats: boolean;
  includeReviews: boolean;
  includeVersions: boolean;
  anonymize: boolean;
}

// 人格导入类型
export interface PersonaImport {
  file: File;
  options: {
    overwrite: boolean;
    preserveId: boolean;
    updateExisting: boolean;
    validateConfig: boolean;
  };
}

// 导出所有类型
export type {
  Persona,
  PersonaConfig,
  PersonaStats,
  PersonaCategory,
  PersonaTag,
  PersonaTemplate,
  PersonaVersion,
  PersonaReview,
  PersonaReviewReply,
  PersonaFavorite,
  PersonaShare,
  PersonaMarketplace,
  PersonaSearchResult,
  PersonaSession,
  PersonaAnalytics,
  PersonaExport,
  PersonaImport,
};
